{
  "cells": [
    {
      "cell_type": "markdown",
      "id": "cell_0",
      "metadata": {},
      "source": [
        "# ContextGem: Aspect Extraction with Sub-Aspects"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_1",
      "metadata": {},
      "outputs": [],
      "source": [
        "%pip install -U contextgem"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "cell_2",
      "metadata": {},
      "source": [
        "To run the extraction, please provide your LLM details in the ``DocumentLLM(...)`` constructor further below."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cell_3",
      "metadata": {},
      "outputs": [],
      "source": [
        "# ContextGem: Aspect Extraction with Sub-Aspects\n",
        "\n",
        "import os\n",
        "\n",
        "from contextgem import Aspect, Document, DocumentLLM\n",
        "\n",
        "\n",
        "# Create a document instance\n",
        "doc = Document(\n",
        "    raw_text=(\n",
        "        \"Employment Agreement\\n\"\n",
        "        \"This Employment Agreement is entered into between Global Tech Inc. (Company) and John Smith (Employee).\\n\"\n",
        "        \"\\n\"\n",
        "        \"Section 8: Termination\\n\"\n",
        "        \"8.1 Termination by Company\\n\"\n",
        "        \"The Company may terminate this agreement at any time with or without cause by providing thirty (30) days \"\n",
        "        \"written notice to the Employee. In case of termination for cause, no notice period is required.\\n\"\n",
        "        \"\\n\"\n",
        "        \"8.2 Termination by Employee\\n\"\n",
        "        \"The Employee may terminate this agreement by providing fourteen (14) days written notice to the Company. \"\n",
        "        \"The Employee must complete all pending assignments before the termination date.\\n\"\n",
        "        \"\\n\"\n",
        "        \"8.3 Severance Benefits\\n\"\n",
        "        \"Upon termination without cause, the Employee shall receive severance pay equal to two (2) weeks of base salary \"\n",
        "        \"for each year of service, with a minimum of four (4) weeks and a maximum of twenty-six (26) weeks. \"\n",
        "        \"Severance benefits are contingent upon signing a release agreement.\\n\"\n",
        "        \"\\n\"\n",
        "        \"8.4 Return of Company Property\\n\"\n",
        "        \"Upon termination, the Employee must immediately return all Company property, including laptops, access cards, \"\n",
        "        \"confidential documents, and any other materials belonging to the Company.\\n\"\n",
        "        \"\\n\"\n",
        "        \"Section 9: Non-Competition\\n\"\n",
        "        \"The Employee agrees not to engage in any business that competes with the Company for a period of twelve (12) \"\n",
        "        \"months following termination of employment within a 50-mile radius of the Company's headquarters.\\n\"\n",
        "    ),\n",
        ")\n",
        "\n",
        "# Define the main termination aspect with sub-aspects\n",
        "termination_aspect = Aspect(\n",
        "    name=\"Termination Provisions\",\n",
        "    description=\"All provisions related to employment termination including conditions, procedures, and consequences\",\n",
        "    aspects=[\n",
        "        Aspect(\n",
        "            name=\"Company Termination Rights\",\n",
        "            description=\"Conditions and procedures for the company to terminate the employee, including notice periods and cause requirements\",\n",
        "        ),\n",
        "        Aspect(\n",
        "            name=\"Employee Termination Rights\",\n",
        "            description=\"Conditions and procedures for the employee to terminate employment, including notice requirements and obligations\",\n",
        "        ),\n",
        "        Aspect(\n",
        "            name=\"Severance Benefits\",\n",
        "            description=\"Compensation and benefits provided to the employee upon termination, including calculation methods and conditions\",\n",
        "        ),\n",
        "        Aspect(\n",
        "            name=\"Post-Termination Obligations\",\n",
        "            description=\"Employee obligations that continue after termination, including property return and non-competition requirements\",\n",
        "        ),\n",
        "    ],\n",
        ")\n",
        "\n",
        "# Add the aspect to the document\n",
        "doc.add_aspects([termination_aspect])\n",
        "\n",
        "# Configure DocumentLLM with your API parameters\n",
        "llm = DocumentLLM(\n",
        "    model=\"azure/gpt-4.1-mini\",\n",
        "    api_key=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_KEY\"),\n",
        "    api_version=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_VERSION\"),\n",
        "    api_base=os.getenv(\"CONTEXTGEM_AZURE_OPENAI_API_BASE\"),\n",
        ")\n",
        "\n",
        "# Extract aspects from the document\n",
        "termination_aspect = llm.extract_aspects_from_document(doc)[0]\n",
        "\n",
        "# Access the extracted information\n",
        "print(\"All Termination Provisions:\")\n",
        "for item in termination_aspect.extracted_items:\n",
        "    print(f\"- {item.value}\")\n",
        "print(\"\\nSub-Aspects:\")\n",
        "for sub_aspect in termination_aspect.aspects:\n",
        "    print(f\"\\n{sub_aspect.name}:\")\n",
        "    for item in sub_aspect.extracted_items:\n",
        "        print(f\"- {item.value}\")\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.10.0"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}